package com.ygo.feihua.service;
import android.app.*;
import android.content.*;
import android.os.*;
import cn.bmob.v3.datatype.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.baidu.location.*;
import com.baidu.mapapi.*;
import com.baidu.mapapi.model.*;
import com.baidu.mapapi.radar.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;

public class ServiceMap extends Service
{
	private LocationClient mLocationClient = null;
	private MyLocationListener myListener ;
	private double jd;

	private double wd;
	private UserManagement um;
	//工具类
	private Gongju gj;
	private RadarSearchManager mManager;
	@Override
	public IBinder onBind(Intent p1)
	{
		// TODO: Implement this method
		return null;
	}

	@Override
	public void onCreate()
	{
		// TODO: Implement this method
		super.onCreate();
		
		gj=Gongju.getdx(this);
		
		um=UserManagement.getDx();
		
		myListener = new MyLocationListener();
		mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
		mLocationClient.registerLocationListener(myListener);    //注册监听函数

		mManager = RadarSearchManager.getInstance();
		initLocation();
		mLocationClient.start();
		//设置自动上传的callback和时间间隔,单位毫秒
		mManager.startUploadAuto(new zsc(),3000000);	
	}
	
	
	//自动上传用户位置信息,用于查找周边决斗者定位
	public class zsc implements RadarUploadInfoCallback
	{
		//实现上传callback，自动上传
		@Override
		public RadarUploadInfo onUploadInfoCallback()
		{	
            //执行定位前需先停止之前的定位
            mLocationClient.stop();
            initLocation();
            mLocationClient.start();
			String yid = "未登录"; 
			if(um.getUser()!=null){
				yid=um.getUser().getUsername();
			}
            mManager.setUserID(null);
			RadarUploadInfo info = new RadarUploadInfo();
			info.comments = yid;
			info.pt = new LatLng(wd,jd);

			return info;
		}
	}
	//定位参数
	private void initLocation()
	{
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
		option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
	}

	//定位回调方法
	public class MyLocationListener implements BDLocationListener
	{

		

		@Override
		public void onReceiveLocation(BDLocation location)
		{	
			jd= location.getLongitude();
			wd= location.getLatitude();
			if (wd == 0 || jd == 0)
			{
				gj.show("定位失败,原因为" + location.getLocType());
				return;
			}

				if(um.getUser()!=null){
                if(jd!=0&&wd!=0){
					MyUser muu=new MyUser();
					muu.setUserpoint(new BmobGeoPoint(jd,wd));
					muu.update(um.getUser().getObjectId(), new UpdateListener(){

							@Override
							public void done(BmobException p1)
							{

								// TODO: Implement this method
							}
						});
				}
			}
		}
	}
	
	@Override
	public void onDestroy()
	{
		// TODO: Implement this method
		super.onDestroy();
		mLocationClient.stop();
		//停止自动上传位置信息
		mManager.stopUploadAuto();
		//释放资源
		mManager.destroy();
		mManager = null;		  
		
	}
	
	
}
